package org.scc.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.scc.model.Resource;
import org.scc.service.ResourceService;

public class ResourceAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private ResourceService resourceService;
	private Resource resource;
	private int pid;
	
	public ResourceAction(){
		resourceService = new ResourceService();
	}

	private String getResJsonTb(List<Resource> resources){
		JSONArray arr = new JSONArray();
		JSONObject obj = null;
		StringBuffer opt = new StringBuffer();
		for(Resource resource : resources){
			obj = new JSONObject();
			obj.put("id", resource.getId() );
			obj.put("name", resource.getName() );
			obj.put("url", resource.getUrl() );
			opt.append("<a href='").append("secondLevelResManager.jsp?id=").append(resource.getId());
			opt.append("' title='二级菜单管理'><img src='../easyui/themes/icons/menu.png'/>");
			obj.put("opt", opt.toString());
			opt.setLength(0);
			arr.add(obj);
		}
		return arr.toString();
	}
	
	public String initFLEditPage(){
		if(id != 0){
			resource = resourceService.load(id);
		}
		return SUCCESS;
	}
	
	public String initSLEditPage(){
		if(id != 0){
			resource = resourceService.load(id);
		}
		return SUCCESS;
	}
	
	public void editResource(){
		if(resource.getId() == 0){
			if(resource.getParentRes() != null){
				resource.setLevel(Resource.LEVEL_SECOND);
			}
			resourceService.add(resource);
		} else {
			if(resource.getParentRes() != null){
				resource.setLevel(Resource.LEVEL_SECOND);
			}
			resourceService.update(resource);
		}
		responseSuccess();
	}

	public void deleteResource(){
		resourceService.delete(ids);
		responseSuccess();
	}
	
	/**
	 * 查询一级菜单
	 */
	public void findFirstLevel(){
		List<Resource> resources = resourceService.findFirstLevel();
		responeString(getResJsonTb(resources)); 
	}
	
	/**
	 * 查询二级菜单
	 */
	public void findSecondLevel(){
		List<Resource> resources = resourceService.findSecondLevel(pid);
		responeString(getResJsonTb(resources)); 
	}
	
	public void findResTree(){
		responeString(getFirstLevelJSON().toString());
	}
	
	private JSONArray getFirstLevelJSON(){
		List<Resource> resources = resourceService.findFirstLevel();
		JSONObject treeNode = null;
		JSONArray nodeArr = new JSONArray();
		for(Resource res : resources){
			treeNode = new JSONObject();
			treeNode.put("id", res.getId() );
			treeNode.put("text", res.getName() );
			treeNode.put("iconCls", "icon-menu" );
			treeNode.put("attributes", "{'level':1}" );
			treeNode.put("children", getSecondLevelJSON(res.getId()));
			nodeArr.add(treeNode);
		}
		return nodeArr;
	}
	
	private JSONArray getSecondLevelJSON(int id){
		List<Resource> resources = resourceService.findSecondLevel(id);
		JSONObject treeNode = null;
		JSONArray nodeArr = new JSONArray();
		for(Resource res : resources){
			treeNode = new JSONObject();
			treeNode.put("id", res.getId() );
			treeNode.put("text", res.getName() );
			treeNode.put("iconCls", "icon-menu" );
			treeNode.put("attributes", "{'level':2}" );
			
			nodeArr.add(treeNode);
		}
		return nodeArr;
	}
	
	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}
	
}
